<?php

namespace app\common\model;

/**
 * [MemberQuan  用户福券表]
 * @author [C。]
 * @return html
 */
class MemberQuan extends Base
{
    protected $name = 'member_quan';

    /**
     * 根据搜索条件获取用户信息
     */
    public function getMembersByConditon($map)
    {
        $join = [
            ['app_member am','q.member_id = am.id'],
        ];

        $result =  $this->field('am.tel,am.username,q.addtime')
            ->alias('q')
            ->join($join)
            ->where($map)
            ->find();

        return $result;
    }

    /**
     * 根据搜索条件获取用户信息
     */
    public function getMemberByConditon($map)
    {
        $join = [
            ['app_member am','q.member_id = am.id'],
        ];

        $result =  $this->field('am.tel,am.username,q.addtime')
            ->alias('q')
            ->join($join)
            ->where($map)
            ->find();

        return $result;
    }

    /**
     * 根据剩余次数信息
     */
    public function getNum($map)
    {
        $join = [
            ['app_member am','q.member_id = am.id'],
        ];

        $result =  $this
            ->alias('q')
            ->join($join)
            ->where($map)
            ->sum('num');

        return $result;
    }
    /**
     * 福券列表信息
     */
    public function getlists($map,$from = 0, $size = 5)
    {
        $order = ['member_quan_id' => 'desc'];

        $result = $this->distinct('jing')->field('member_quan_id,jing,num,member_id,addtime')->where($map)->limit($from, $size)->order($order)->select()->toArray();
        return $result;
    }
    /**
     * 根据条件来获取列表的数据的总数
     * @param array $param
     */
    public function getCountByCondition($map = []) {
        return $this->where($map)->count();
    }

    /**
     * [editData  通用更新数据]
     * @author [C。]
     * @param $data
     * @return mixed
     */
    public function editNum($data)
    {
        if(!is_array($data))
            exception('传递数据不合法');

        $this->allowField(true)->isUpdate(true)->save($data, ['member_quan_id' => $data['member_quan_id']]);

        return $this->member_quan_id;
    }
}